package com.dangz.dans.service;

import com.dangz.dans.dao.mapper.UserMapper;
import com.dangz.dans.dao.repository.UserRepository;
import com.dangz.dans.model.Org;
import com.dangz.dans.model.User;
import com.dangz.dans.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * UserServiceImpl 用户实现
 * @author dangz
 * @since 2017/12/18 16:07
 */
@Service
public class UserServiceImpl extends EntityServiceImpl<User> {
    @Autowired
    private UserRepository userRepository;
    @Autowired(required = false)
    private UserMapper userMapper;

    public PageUtil<User> listPage(Integer curPage, Integer pageSize, final String userName) {
        curPage = curPage == null ? 1 : curPage;
        curPage = curPage == null ? 10 : curPage;
        Pageable pageable = new PageRequest(curPage-1, pageSize, new Sort(Sort.Direction.ASC, "userId"));
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if(StringUtils.isNotEmpty( userName )) {
                    list.add(cb.like(root.get("userName").as(String.class), "%"+ userName + "%"));
                }
                Join<User, Org> orgJoin = root.join(root.getModel().getSingularAttribute("org"), JoinType.LEFT);
//                list.add(cb.equal(root.get("orgId"), orgJoin.get("orgId")));
                list.add(cb.equal(orgJoin.get("orgType"), "1"));
                Predicate[] p = new Predicate[list.size()];
                return query.where(list.toArray(p)).getRestriction();
            }
        };
        return new PageUtil<User>( userRepository.findAll(specification, pageable ) );
    }

    /**
     * MethonName: saveOrUpdate
     * 保存或更新
     * @params:  [user]
     * @return:  com.dangz.model.User
     *
     * @author: dangz
     * @since: 2017/12/18 16:09
     */
    public User saveOrUpdate(User user){
        return userRepository.save(user);
    }

    /**
     * MethonName: saveOrUpdate
     * 查找
     * @params:  [userId]
     * @return:  com.dangz.model.User
     *
     * @author: dangz
     * @since: 2017/12/18 16:09
     */
    public User findOne(String userId){
        return userMapper.findByUserId(userId);
//        return userRepository.findOne(userId);
    }

    public List<User> findOneSql(String userId) {
        /*EntityManager entityManager = containerEntityManagerFactoryBean.getNativeEntityManagerFactory().createEntityManager();
        Query query = entityManager.createNativeQuery("SELECT user_id as userId, user_name as userName FROM user WHERE user_id = ?");
        query.setParameter(1, userId);
        EntityManager manager = em.createEntityManager();

        query = manager.createQuery("FROM User WHERE userId = ?");
        query.setParameter(1, userId);*/
        return findAll(User.class);
    }

}
